import io.netty.buffer.ByteBuf;
import io.netty.buffer.PooledByteBufAllocator;
import io.netty.buffer.Unpooled;
import utils.ByteUtil;

import java.io.IOException;
import java.nio.charset.Charset;
import java.time.Instant;


public class Test2 {
    public static void main(String[] args) throws IOException {

//        String path = "D:\\GitintellijProject\\xny\\gb32960Parse\\testData.txt";
       // byte[] buf = Util.readTxt(path);
        String test="232302FE4C47415833503133584D3630303031393101024A150C03103330010103010000000014E815D936C16101000AE3000002010101464E204E204715F42710050006B9C51801AEC9FC0601B50D0301030D000128450120400700000000000000000008010115D936C101500001C80D010D010D000D000D010D010D010D010D000D020D010D010D020D010D010D010D010D010D010D010D000D010D010D010D010D010D010D010D000D010D010D010D010D010D010D020D010D020D020D020D020D020D010D010D010D010D010D010D020D010D010D020D020D020D020D020D010D010D010D010D010D010D010D010D010D020D010D020D020D020D010D010D000D000D010D010D010D000D010D010D010D010D010D020D000D010D010D000D010D010D010D010D010D010D020D020D020D020D010D000D010D010D010D000D010D010D010D010D020D010D010D020D010D000D000D010D010D000D010D010D000D010D010D020D020D010D010D010D010D010D010D010D010D010D010D020D020D020D020D020D010D010D010D010D010D010D010D000D010D020D010D010D010D020D010D010D010D010D010D010D010D010D010D020D020D020D020D020D000D010D000D010D010D010D010D010D010D020D020D020D030D020D010D010D010D010D010D010D010D010D010D020D020D020D020D020D010D010D010D0109010100384343444343434344444443444344414343414142424343434243424241444240414242434444424542434243424141424143444342444243820005444653593383001A00010009000444465359332D322E30302D4534430000000000009F";
        ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
        buffer.writeBytes(ByteUtil.hexStringToBytes(test));
        decode(buffer);
    }


    /**
     * 协议解码 只负责解码数据包
     * 数据单元解码由ParseHandler完成
     * @return
     */
    
    public static Object decode(ByteBuf byteBuf) {
       // EvGBProtocol protocol = new EvGBProtocol();
        byteBuf.markReaderIndex();
        byte[] hexArray = new byte[byteBuf.writerIndex()];
        byteBuf.readBytes(hexArray);
        //protocol.setHex(ByteUtil.byteToHex(hexArray));
        System.out.println(ByteUtil.byteToHex(hexArray));
        byteBuf.resetReaderIndex();
        Boolean checkBcc =checkBcc(byteBuf);
       // protocol.setBcc(checkBcc);
        System.out.println("checkBcc="+checkBcc);
        Boolean checkBegin = Constants.BEGIN.equals(byteBuf.readSlice(2).toString(Charset.forName(Constants.UTF_8)));
       // protocol.setBegin(checkBegin);
        System.out.println("checkBegin="+checkBegin);
        //protocol.setCommandType(CommandType.valuesOf(byteBuf.readUnsignedByte()));
        CommandType commandType = CommandType.valuesOf(byteBuf.readUnsignedByte());
        System.out.println("CommandType="+ commandType);
       // protocol.setResponseType(ResponseType.valuesOf(byteBuf.readUnsignedByte()));
        System.out.println("ResponseType="+ResponseType.valuesOf(byteBuf.readUnsignedByte()));
       // protocol.setVin(byteBuf.readSlice(17).toString(Charset.forName(Constants.UTF_8)));
        System.out.println("vin="+byteBuf.readSlice(17).toString(Charset.forName(Constants.UTF_8)));
       // protocol.setEncryptionType(EncryptionType.valuesOf(byteBuf.readUnsignedByte()));
        System.out.println("EncryptionType="+EncryptionType.valuesOf(byteBuf.readUnsignedByte()));
        //校验码正确与起始符号正确的时候才解析数据单元
        if(checkBcc && checkBegin){
            int length = byteBuf.readUnsignedShort();
            System.out.println("length="+length);
           // protocol.setLength(length);
            if(length>0){
                //protocol.setBody(new DataBody(byteBuf.readSlice(length)));
                new DataBody().decode(byteBuf.readSlice(length),commandType);
                //System.out.println(byteBuf.readSlice(length));
            }
        }
       // protocol.setGatewayReceiveTime(Instant.now().toEpochMilli());
        System.out.println(Instant.now().toEpochMilli());
        byteBuf.readUnsignedByte();
        return null;
    }

    /**
     * BCC校验(异或校验)
     * @param byteBuf
     * @return
     */
    public static byte signBcc(ByteBuf byteBuf) {
        byte cs = 0;
        while (byteBuf.isReadable()){
            cs ^= byteBuf.readByte();
        }
        byteBuf.resetReaderIndex();
        return cs;
    }

    /**
     * 校验BCC是否正确
     * @param byteBuf
     * @return
     */
    public static Boolean checkBcc(ByteBuf byteBuf) {
        byte bcc = byteBuf.getByte(byteBuf.readableBytes()-1);
        ByteBuf slice = byteBuf.slice(2, byteBuf.readableBytes()-3);
        byte checkBcc = signBcc(slice);
        byteBuf.resetReaderIndex();
        return bcc == checkBcc;
    }
}
